<?php
namespace app\admin\model;

use think\Model;

/**
 * 通道黑名单模型
 * @package app\admin\model
 */
class WarUrl extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'war_check_url';

    //更新
    public static function upd($data, $id)
    {
        if(isset($data['post_url'])){
            $data['post_url'] = html_entity_decode($data['post_url']);
        }
        if(isset($data['target_url'])){
            $data['target_url'] = html_entity_decode($data['target_url']);
        }
        return self::where(['id' => $id])->update($data);
    }

    public static function add($data)
    {
        if(isset($data['post_url'])){
            $data['post_url'] = html_entity_decode($data['post_url']);
        }
        if(isset($data['target_url'])){
            $data['target_url'] = html_entity_decode($data['target_url']);
        }
        $data['create_time'] = date('Y-m-d H:i:s', time());
        return self::create($data);
    }

    //渠道预警开关
    public static function openOrClose($id, $status)
    {
        trace(session('username') . '把渠道预警id为' . $id . '的开启状态改为了' . $status, 'info');
        return self::where(['id' => $id])->update(['onoff' => $status, 'war_count' => 0]);
    }

    /**
     * [getAlls 条件查询全部数据]
     * @author   .guo
     * @DataTime 2019-05-27
     * @param    [type]     $request [条件数组]
     * @return   [type]              [object]
     */
    public static function getAlls($request)
    {
        $where = [];
        if ($request['cid'] != '') {
            $where[] = ['wu.cid', 'eq', $request['cid']];
        }
        return self::alias('wu')
            ->field('wu.*,bc.company_name')
            ->where($where)
            ->join('bus_channel bc', 'wu.cid = bc.channel_id', 'LEFT')
            ->paginate($request['limit'], false, $request)
            ->each(function ($item, $key) {
                $item->company_name = $item->company_name . '/' . $item->cid;
                switch ($item->onoff) {
                    case '1':
                        $item->onoff_ = '<span class="layui-badge layui-bg-green status-qy">开启</span>';
                        break;

                    default:
                        $item->onoff_ = '<span class="layui-badge status-jy">禁用</span>';
                        break;
                }
                switch ($item->phone_type) {
                    case '0':
                        $item->phone_type = '<span class="layui-badge">安卓</span>';
                        break;
                    case '1':
                        $item->phone_type = '<span class="layui-badge layui-bg-green">苹果</span>';
                        break;

                    default:
                        $item->phone_type = '未知';
                        break;
                }
            });

    }

}
